package graphicUserInterface.testAndCalibrationInterfaces.screens;

import graphicUserInterface.elements.WebCamDisplay;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;

import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

public class CalibrationToolScreenLocal extends JPanel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1497045403403127643L;
	
	/* *********************************************************
	 * Attributes
	 * ********************************************************* */
	private JPanel _leftPart;
	private JPanel _rightPart;
	private WebCamDisplay _webCamContainer;
	
	/* *********************************************************
	 * Constructor(s)
	 * ********************************************************* */
	/**
	 * 
	 */
	public CalibrationToolScreenLocal(){
		super();
		System.out.println("calibrationToolScreen creation");
		this.setLayout(new BoxLayout(this, BoxLayout.X_AXIS));
		_webCamContainer = new WebCamDisplay();
		_leftPart = new JPanel();
		this.drawCam();
		_rightPart = new JPanel();
		this.drawTrackingInfo();
		
		this.add(_leftPart);
		
		this.add(_rightPart);
		
		this.validate();
		System.out.println("calibrationToolScreen creation over");
	}
	
	/* *********************************************************
	 * Instance Methods
	 * ********************************************************* */

	

	private void drawTrackingInfo() {
		_rightPart.add(new JLabel("nothing to put here for now"));
		//TODO met ce qu'il te plait ici manu ....
		JButton toto = new JButton("calibrate");
		


		toto.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
					System.out.println("Game on!!\n");
					//values
					int margin = 5;
					int low_margin = 200;
					int high_margin = 307000;
					
					BufferedImage image = engine.videoCapture.GrabFrame
						.doGrabFrame(_webCamContainer.getFrameGrabbingControl());
					
					int [] average = engine.motionDetection.CalibrationLocal.calibrate(image);
					System.out.println("Average color is " + Integer.toHexString(average[0]) + " " + Integer.toHexString(average[1]) + " " + Integer.toHexString(average[2])); 
					System.out.println("Average color is " + Integer.toHexString(average[0]-margin) + " " + Integer.toHexString(average[1]-margin) + " " + Integer.toHexString(average[2]-margin));
					System.out.println("Average color is " + Integer.toHexString(average[0]+margin) + " " + Integer.toHexString(average[1]+margin) + " " + Integer.toHexString(average[2]+margin));
					boolean  isOK = engine.motionDetection.CalibrationLocal.isCalibrationOK(image, margin, average, low_margin, high_margin);
					if(isOK){
						System.out.println("You won!!\n");
					}
					else{
						System.out.println("Game over!!\n");
					}
					_webCamContainer.setCalibrationZone(false);
					_webCamContainer.setTrackerSquare(true);
					while (true){
					image = engine.videoCapture.GrabFrame.doGrabFrame(_webCamContainer.getFrameGrabbingControl());	
					
					int [] coord = engine.motionDetection.HandTrackingLocal.track(image,margin, average, low_margin, high_margin);
					
					if((coord[0]!=-1)&&(coord[1]!=-1)){
						_webCamContainer.setTrackingPosition(coord[0], coord[1]);
					}}
			}
		});

		_rightPart.add(toto);
		
		//something to modify the margin for the color
		
		//something to modify the margins for the number of pixels
		
		//a button to launch the calibration
		
		//the 3 colors
		
		//if the calibration was ok
		
		
		
	}

	/**
	 * 
	 */
	private void drawCam() {
		_leftPart.add(_webCamContainer);
		_webCamContainer.setCalibrationZone(true);
	}
}
